/* eslint-disable consistent-return */
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
// import Home from '@/views/Home.vue';
import Layout from '@/components/Layout.vue';
import famasIn from '@/router/friamin';
import friamOut from '@/router/friamOut';
import store from '@/store/index';
import { showMsg } from '@/uitl/toast';

const routes : Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Layout',
    component: Layout,
    redirect: '/index',
    children: famasIn,
  },
  ...friamOut,
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((after, next) => {
  console.log('state', store.state);
  const { path } = after;
  if (path === '/index' || path === '/long') return true;
  const user = window.localStorage.getItem('user');
  if (!user) {
    showMsg('请先登录');
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    return (next as any)({ name: '/long' });
  }
  // eslint-disable-next-line
  const canEnter = store.state.memu.find((item) => (item as any).url === path);
  if (canEnter) return true;
  return false;
});
export default router;
